﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Capstone_project.BussnessLogicLayer;
using Capstone_project.DatabaseAccessLayer;
using System.Data.SqlClient;

namespace Capstone_project
{
    public partial class frmAssignTask : Form
    {
        StaffBUS staffBUS = new StaffBUS();
        TaskBUS taskbus = new TaskBUS();
        User_taskBUS usertaskbus=new User_taskBUS();
        public frmAssignTask()
        {
            InitializeComponent();
        }
        SqlConnection con = Common.con;
        DataTable dt = new DataTable();
        DataTable dt1 = new DataTable();
        SqlDataAdapter adap;
        private void frmAssignTask_Load(object sender, EventArgs e)
        {
            string sql = "SELECT     dbo.TASK.ID_TASK, dbo.TASK.NAME_TASK, dbo.PROJECT.NAME_PROJECT, dbo.TASK_GROUP.NAME_TASK_GROUP, dbo.PRIORITY.NAME_PRIORITY, "
                      +" dbo.ASSIGN_TASK_STATUS.STATUS, dbo.TASK.TIMES"
                      +" FROM         dbo.TASK INNER JOIN"
                      +" dbo.TASK_GROUP ON dbo.TASK.ID_TASK_GROUP = dbo.TASK_GROUP.ID_TASK_GROUP INNER JOIN"
                      +" dbo.PROJECT ON dbo.TASK_GROUP.ID_PROJECT = dbo.PROJECT.ID_PROJECT INNER JOIN"
                      +" dbo.PRIORITY ON dbo.TASK.ID_PRIORITY = dbo.PRIORITY.ID_PRIORITY INNER JOIN"
                      +" dbo.ASSIGN_TASK_STATUS ON dbo.TASK.ID_TASK_STATUS = dbo.ASSIGN_TASK_STATUS.ID_TASK_STATUS";
            if (con.State != ConnectionState.Open)
                con.Open();
            adap = new SqlDataAdapter(sql, con);
            dt = new DataTable();
            adap.Fill(dt);
            con.Close();
            BindingSource bdsource = new BindingSource();
            bdsource.DataSource = dt;
            bindingNavigator1.BindingSource = bdsource;
            
            txtIDTASK.DataBindings.Add("Text", bdsource, "ID_TASK");
            txtNameTASK.DataBindings.Add("Text", bdsource, "NAME_TASK");
            txtPROJECT.DataBindings.Add("Text", bdsource, "NAME_PROJECT");
            txtTASKGROUP.DataBindings.Add("text", bdsource, "NAME_TASK_GROUP");
            txtPRIORITY.DataBindings.Add("Text", bdsource, "NAME_PRIORITY");
            txtTime.DataBindings.Add("Text",bdsource,"TIMES");

            dt1.Rows.Clear();
            string sqldt1 = "SELECT     USER_NAME, START_DATE, END_DATE, DESCRIPTION"
            +" FROM         dbo.USER_TASK WHERE (USER_TASK.ID_TASK='" + txtIDTASK.Text + "')";
            if (con.State != ConnectionState.Open)
                con.Open();
            adap = new SqlDataAdapter(sqldt1, con);
            adap.Fill(dt1);
            con.Close();
            BindingSource bdsource1 = new BindingSource();
            bdsource1.DataSource = dt1;
            dataGridView1.DataSource = bdsource1;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            bindingNavigator2.BindingSource = bdsource1;

            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                if (i % 2 == 0)
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
                }
            }

            dateTimePickerStartDate.MinDate = DateTime.Today;
            dateTimePickerStartDate.Text = "";

            //fill data into combobox username
            string sqlusername = "SELECT [USER_NAME] FROM [databaseDRM].[dbo].[USER]";
            if (con.State != ConnectionState.Open)
                con.Open();
            SqlCommand com = new SqlCommand(sqlusername, con);
            adap = new SqlDataAdapter(com);
            DataTable dtUsername = new DataTable();
            adap.Fill(dtUsername);
            con.Close();
            cbxUsername.DataSource = dtUsername;
            cbxUsername.ValueMember = "USER_NAME";
            cbxUsername.DisplayMember = "USER_NAME";
            
        }

        private void txtIDTASK_TextChanged(object sender, EventArgs e)
        {
            dt1.Rows.Clear();
            string sql = "SELECT     USER_NAME, START_DATE, END_DATE, DESCRIPTION"
            +" FROM         dbo.USER_TASK WHERE (USER_TASK.ID_TASK='"+txtIDTASK.Text+"')";
            SqlConnection con = Common.con;
            adap = new SqlDataAdapter(sql, con);
            adap.Fill(dt1);
            dataGridView1.DataSource = dt1;
        }

        private void btnAssigntask_Click(object sender, EventArgs e)
        {
            groupBoxAssignto.Enabled = true;
            btnOK.Visible = true;
            btnAssigntask.Visible = false;
            btnCancel.Enabled = true;
        }

        private void dateTimePickerStartDate_ValueChanged(object sender, EventArgs e)
        {
            float times=float.Parse(txtTime.Text);
            dateTimePickerEnddate.Value = dateTimePickerStartDate.Value.AddHours(times);
            
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (Common.CheckTaskAssignedtoUN(txtIDTASK.Text, cbxUsername.SelectedValue.ToString()) == false)
                {
                    MessageBox.Show("Task assign is duplicate, Please try again.");
                    cbxUsername.Focus();
                }
                else
                {
                    if (con.State != ConnectionState.Open)
                        con.Open();
                    string sql = "INSERT INTO USER_TASK (USER_NAME,ID_TASK,START_DATE,END_DATE,COMPLETE,DESCRIPTION) VALUES ('" + cbxUsername.SelectedValue + "','" + txtIDTASK.Text + "','" + dateTimePickerStartDate.Value + "','" + dateTimePickerEnddate.Value + "',0,'" + txtComment.Text + "')";
                    SqlCommand com = new SqlCommand(sql, con);
                    int kq = com.ExecuteNonQuery();
                    if (kq != 0)
                    {
                        MessageBox.Show("Assign successful.");
                        btnAssigntask.Visible = true;
                        btnOK.Visible = false;
                        txtComment.Text = "";
                        dateTimePickerStartDate.ResetText();
                        groupBoxAssignto.Enabled = false;
                        refreshgridview();
                        if (con.State != ConnectionState.Open)
                            con.Open();
                        string sql1 = "UPDATE TASK SET ID_TASK_STATUS='ATS002' WHERE ID_TASK='" + txtIDTASK.Text + "'";
                        SqlCommand com1 = new SqlCommand(sql1, con);
                        com1.ExecuteNonQuery();
                    }
                    con.Close();
                }
            }
            catch { MessageBox.Show("Cannot assign."); }
        }
        private void refreshgridview()
        {
            dt1.Rows.Clear();
            string sql = "SELECT     USER_NAME, START_DATE, END_DATE, DESCRIPTION"
            + " FROM USER_TASK WHERE (USER_TASK.ID_TASK='" + txtIDTASK.Text + "')";
            if (con.State != ConnectionState.Open)
                con.Open();
            adap = new SqlDataAdapter(sql, con);
            adap.Fill(dt1);
            con.Close();
            dataGridView1.DataSource = dt1;
        }
        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to save changes ?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    SqlCommandBuilder s = new SqlCommandBuilder(adap);
                    dt1.AcceptChanges();
                    int kq = adap.Update(dt1);
                    dataGridView1.DataSource = dt1;
                    if (kq != 0)
                    {
                        MessageBox.Show("Changes to be saved");
                    }
                    else { MessageBox.Show("Fail to save"); }
                }
                catch
                {
                    dataGridView1.Refresh();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnOK.Visible = false;
            btnAssigntask.Visible = true;
            btnCancel.Enabled = false;
        }
    }
}
